package com.zookeeper;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * Created by peak on 2015/11/30.
 */
public class Test1 {
    public static void main(String[] args) throws Exception {
        final CountDownLatch connectedLatch = new CountDownLatch(1);
        System.out.println(Thread.currentThread().getName());
        ZooKeeper zk = new ZooKeeper("localhost:2181", 50000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                System.out.println("process: "+Thread.currentThread().getName());
                synchronized (connectedLatch){
                    connectedLatch.notify();
                }
            }
        });

        while(true){
            List<String> list =  zk.getChildren("/test1",true);
            System.out.println(list.size()+" wait ...");
            synchronized (connectedLatch){
                connectedLatch.wait();
            }
        }


//        CountDownLatch latch = new CountDownLatch(1);
//        latch.await();
    }
}
